﻿Imports System.Data.OleDb
Partial Public Class newArticleTags
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Article_Title.Text = Request.Params("Article_Title")
        Dim ArticleID = Request.Params("nID")

        ArticleIdField.Value = ArticleID


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)

        oleDbConn.Open()

        Dim cmdQuery As String = "SELECT Tag from Tags"

        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)

        Dim dbread = dbcomm.ExecuteReader()

        While dbread.Read()
            Dim hlink As New HyperLink
            Dim tagName = dbread.Item("Tag")
            If tagName <> "All" Then
                hlink.Text = tagName
                hlink.NavigateUrl = "javascript:addTagToArticle('" & hlink.Text & "')"
                ExistingTags.Controls.Add(hlink)
                ExistingTags.Controls.Add(New LiteralControl("&nbsp;&nbsp;"))
            End If

        End While

        dbread.Close()
        oleDbConn.Close()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim ArticleID = ArticleIdField.Value
        Dim tagsTheUserEntered = TagField.Text

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
        oleDbConn.Open()
        Dim cmdQuery As String = "SELECT Tag from Tags"
        Dim dbcomm = New OleDbCommand(cmdQuery, oleDbConn)
        Dim dbread = dbcomm.ExecuteReader()
        Dim allTagsConcatenated = ""
        While dbread.Read()
            allTagsConcatenated = allTagsConcatenated & dbread.Item("Tag")
        End While


        Dim newTags As ArrayList = New ArrayList()
        Dim oldTags As ArrayList = New ArrayList()
        Dim tagArray = tagsTheUserEntered.Split(" ")

        For i = 0 To tagArray.Length - 1
            If allTagsConcatenated.Contains(tagArray(i)) Then
                oldTags.Add(tagArray(i))
            Else
                newTags.Add(tagArray(i))
            End If

        Next

        oldTags.Add("All")

        Dim addTagCmd As OleDbCommand = New OleDbCommand _
        ("Insert into Tags(Tag) Values (?)", oleDbConn)
        addTagCmd.CommandType = CommandType.Text

        Dim addTagPaperJuncRcd As OleDbCommand = New OleDbCommand _
        ("Insert into JuncTagPapers (Tags_fk_ID,Papers_fk_ID) Values(?,?)", oleDbConn)
        addTagPaperJuncRcd.CommandType = CommandType.Text

        Dim getLatestRcdCmd As OleDbCommand = New OleDbCommand _
        ("SELECT @@IDENTITY", oleDbConn)
        getLatestRcdCmd.CommandType = CommandType.Text

        Dim findTagCmd As OleDbCommand = New OleDbCommand _
        ("SELECT ID From Tags WHERE Tag = ?", oleDbConn)
        findTagCmd.CommandType = CommandType.Text

        For i = 0 To newTags.Count - 1
            addTagCmd.Parameters.Clear()
            addTagCmd.Parameters.AddWithValue("Tag", newTags(i))
            addTagCmd.ExecuteNonQuery()
            Dim nId As Integer = getLatestRcdCmd.ExecuteScalar()
            addTagPaperJuncRcd.Parameters.Clear()
            addTagPaperJuncRcd.Parameters.AddWithValue("Tags_fk_ID", nId)
            addTagPaperJuncRcd.Parameters.AddWithValue("Papers_fk_ID", ArticleID)
            addTagPaperJuncRcd.ExecuteNonQuery()
        Next


        For i = 0 To oldTags.Count - 1
            findTagCmd.Parameters.Clear()
            findTagCmd.Parameters.AddWithValue("Tag", oldTags(i))
            Dim tagReader = findTagCmd.ExecuteReader()
            tagReader.Read()
            If tagReader.HasRows() Then
                Dim nID As Integer = tagReader("ID")
                addTagPaperJuncRcd.Parameters.Clear()
                addTagPaperJuncRcd.Parameters.AddWithValue("Tags_fk_ID", nID)
                addTagPaperJuncRcd.Parameters.AddWithValue("Papers_fk_ID", ArticleID)
                addTagPaperJuncRcd.ExecuteNonQuery()
            End If
            tagReader.Close()
        Next


        Response.Redirect("mobilestandards.aspx")
        oleDbConn.Close()


    End Sub
End Class